package com.cicigodd.brick.plug.system.role.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cicigodd.brick.plug.common.exception.BusinessException;
import com.cicigodd.brick.plug.common.factory.PageFactory;
import com.cicigodd.brick.plug.common.factory.PageResultFactory;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicigodd.brick.plug.system.role.entity.SysRoleUser;
import com.cicigodd.brick.plug.system.role.response.SysRoleUserDetailResponse;
import com.cicigodd.brick.plug.system.role.response.SysRoleUserListResponse;
import com.cicigodd.brick.plug.system.role.request.SysRoleUserAddRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleUserDeleteRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleUserDetailRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleUserEditRequest;
import com.cicigodd.brick.plug.system.role.request.SysRoleUserListRequest;
import com.cicigodd.brick.plug.system.role.mapper.SysRoleUserMapper;
import com.cicigodd.brick.plug.system.role.service.SysRoleUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

/**
 * 角色用户关联
 *
 * @author cicigodd
 * @date 2022/12/12 15:52:23
 */
@Service
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, SysRoleUser> implements SysRoleUserService {

    /**
     * 添加
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysRoleUserAddRequest sysRoleUserAddRequest) {
        SysRoleUser sysRoleUser = new SysRoleUser();
        BeanUtil.copyProperties(sysRoleUserAddRequest, sysRoleUser);
        this.save(sysRoleUser);
    }

    /**
     * 删除
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysRoleUserDeleteRequest sysRoleUserDeleteRequest) {
        this.removeByIds(sysRoleUserDeleteRequest.getIds());
    }


    /**
     * 编辑
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysRoleUserEditRequest sysRoleUserEditRequest) {
        SysRoleUser sysRoleUser = this.getById(sysRoleUserEditRequest.getId());
        if (ObjectUtil.isEmpty(sysRoleUser)) {
            throw new BusinessException("查询结果不存在");
        }
        BeanUtil.copyProperties(sysRoleUserEditRequest, sysRoleUser);
        this.updateById(sysRoleUser);
    }

    /**
     * 详情
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    public SysRoleUserDetailResponse detail(SysRoleUserDetailRequest sysRoleUserDetailRequest) {
        SysRoleUser sysRoleUser = this.getById(sysRoleUserDetailRequest.getId());
        if (ObjectUtil.isEmpty(sysRoleUser)) {
            throw new BusinessException("查询结果不存在");
        }
        SysRoleUserDetailResponse sysRoleUserDetailResponse = new SysRoleUserDetailResponse();
        BeanUtil.copyProperties(sysRoleUser, sysRoleUserDetailResponse);
        return sysRoleUserDetailResponse;
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/12/12 15:52:23
     */
    @Override
    public PageResult<SysRoleUserListResponse> list(SysRoleUserListRequest sysRoleUserListRequest) {
        LambdaQueryWrapper<SysRoleUser> wrapper = new LambdaQueryWrapper<>();
        Integer roleId = sysRoleUserListRequest.getRoleId();
        Integer userId = sysRoleUserListRequest.getUserId();

        wrapper.eq(ObjectUtil.isNotNull(roleId), SysRoleUser::getRoleId, roleId);
        wrapper.eq(ObjectUtil.isNotNull(userId), SysRoleUser::getUserId, userId);
        IPage<SysRoleUserListResponse> page = this.page(PageFactory.defaultPage(sysRoleUserListRequest.getPageNum(),sysRoleUserListRequest.getPageSize()), wrapper).convert(SysRoleUser -> BeanUtil.copyProperties(SysRoleUser, SysRoleUserListResponse.class));
        return PageResultFactory.createPageResult(page);
    }

    /**
     * 根据用户id删除角色
     * @param userId
     */
    @Override
    public void deleteByUserId(Integer userId) {
        LambdaQueryWrapper<SysRoleUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(userId), SysRoleUser::getUserId, userId);
        this.remove(wrapper);
    }

    @Override
    public List<Integer> getRoleIdsByUserId(Integer userId) {
        return this.baseMapper.getRoleIdsByUserId(userId);
    }
}
